$(function() {
	admin.supplier.init();
});
Namespace('admin.supplier', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
				/*var row = $('#datagrid').datagrid('getSelected');
				admin.supplier.edit(row.id);*/
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
	},
	toAdd : function() {
		top.addTab('#tabs','添加供应商', contextPath+'/mall/supplier/add');
	},
	edit:function(id) {
		if(id==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		top.addTab(id,'供应商编辑', contextPath+'/mall/supplier/update/'+id);
	},
	addImg:function(id){
		getDialog('query-dialog').dialog({
			href:contextPath+'/mall/supplier/imgUpload',
			title: '选择文件',
		    width: 350,
		    height: 250,
		    buttons:[{
				text:'上传',
				handler:function(){
					uploaderStart(id);
				}
			}]
		});
	},
	chooseCommunity:function() { //选择社区
		getDialog('query-dialog').dialog({
			href:contextPath+'/community/base/choose',
			title: '点击选择社区',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.supplier.addCommunity();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addCommunity:function() { //选择社区
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		if(rows.length > 1){  
			alert("只能选择一行数据!");  
			return false; 
		}
		var communityId = rows[0].id;
		var communityName = rows[0].name;
		$('#communityId').val(communityId);
		$('#communityName').val(communityName);
	},
	/*selectUser:function(id) {
		getDialog('query-dialog').dialog({
			href:contextPath+'/mall/supplier/user/select/'+id,
			title: '选择关联用户',
		    width: 600,
		    height: 450,
		    onClose: function () {   
		        $(this).dialog('destroy');//销毁  
		    	$('#datagrid').datagrid('reload');  
		    } 
		});
	},*/
	chooseUser:function() { //选择用户
		getDialog('query-dialog').dialog({
			href:contextPath+'/user/base/choose',
			title: '点击选择用户',
		    width: 900,
		    height: 500,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.supplier.addUser();
					$('#query-dialog').dialog('close');
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#query-dialog').dialog('close');
				}
			}]
		});
	},
	addUser:function() {
		var rows = $('#datagrid2').datagrid('getSelections');
		if(rows.length==0){  
			alert("请至少选择一行数据!");  
			return false;  
		}
		if(rows.length > 1){  
			alert("只能选择一行数据!");  
			return false; 
		}
		var userId = rows[0].id;
		var userNick = rows[0].nick;
		$('#userId').val(userId);
		$('#userInfo').val(userNick);
	}
});